/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.postbar.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class MD5Util {

    static private final Logger logger = Logger.getLogger(MD5Util.class.getName());

    public static String getMD5ByFile(File file) {
        String value = "";
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            MappedByteBuffer mbb = fis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(mbb);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString();
        } catch (Exception e) {
            logger.error("get MD5 by file failed,cause: " + e.getMessage(), e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    logger.error("FileInputStream close error," + ex.getMessage(), ex);
                }
            }
        }
        return value;
    }

    public static String getMD5ByString(String str) {
        StringBuffer sbf = new StringBuffer(40);
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digestBytes = md5.digest(str.getBytes());

            for (byte x : digestBytes) {
                if ((x & 0xff) >> 4 == 0) {
                    sbf.append("0").append(Integer.toHexString(x & 0xff));
                } else {
                    sbf.append(Integer.toHexString(x & 0xff));
                }
            }
        } catch (Exception e) {
            logger.error("get MD5 by String failed,cause: " + e.getMessage(), e);
        }
        return sbf.toString();
    }

    public static String[] RetCmdResult(String cmdline) throws IOException {
        String line = "";
        Process process = Runtime.getRuntime().exec(cmdline);
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        line = input.readLine();
        input.close();
        line = line.trim();
        return line.split(" ");
    }
    public static String getMd5(String str) {
        if (str != null) {
            return DigestUtils.md5Hex(str);
        } else {
            return "";
        }
    }
    public static void main(String[] args) throws IOException {
        String path = "C:/Users/Administrator/Desktop/test/1.txt";
//        File file = new File(path);
//        String mD5ByFile = getMD5ByFile(file);
//        System.out.println(mD5ByFile);

        String[] resCmd = RetCmdResult("md5sum " + path);
        String mD5ByFile = resCmd[0];
        System.out.println(mD5ByFile);
    }

}
